package cz.muni.fi.pa165.creatures.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * This class represents a region in which a {@link Creature} lives.
 *
 * @author xmiklos1
 */
@Entity
@Table(name = "REGION")
public class Region implements Serializable {

    /**
     * Id of the region where creatures live.
     */
    private Long id;
    
    /**
     * The name of the region which has to be set in order to persist it.
     */
    private String name;
    
    /**
     * Description of the area.
     */
    private String description;
    
    /**
     * Area of the region (e.g. in square kilometers)
     */
    private Long area;
    
    /**
     * Creatures which lives in a region.
     */
    private List<Creature> creatures = new ArrayList<Creature>();

    /**
     * Default constructor
     */
    public Region() {
    }

    /**
     *
     * @param name name of the region
     */
    public Region(String name) {
        this.name = name;
    }

    /**
     * Gets the id of the region.
     *
     * @return id of the region
     */
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "ID", nullable = false)
    public Long getId() {
        return id;
    }

    /**
     * Sets the id of the region.
     *
     * @param id
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * Gets the name of the region.
     *
     * @return name of the region
     */
    @Column(name = "NAME", nullable = false, length=255)
    public String getName() {
        return name;
    }

    /**
     * Sets the name of the region.
     *
     * @param name name of the region
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Gets the description of the region.
     *
     * @return description of the region
     */
    @Column(name = "DESCRIPTION", length = 512)
    public String getDescription() {
        return description;
    }

    /**
     * Sets the description of the region.
     *
     * @param description description of the region
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * Gets the area of the region.
     *
     * @return area of the region
     */
    @Column(name = "AREA")
    public Long getArea() {
        return area;
    }

    /**
     * Sets the area of the region.
     *
     * @param area area of the region
     */
    public void setArea(Long area) {
        this.area = area;
    }

    /**
     * Gets all creatures which live in the region.
     *
     * @return creatures which live in the region
     */
    @OneToMany(mappedBy = "region")
    public List<Creature> getCreatures() {
        return creatures;
    }

    public void setCreatures(List<Creature> creatures) {
        this.creatures = creatures;
    }
    
    @Override
    public String toString() {
        return "Region{" + "id=" + id + ", name=" + name + ", description="
                + description + ", area=" + area + ", creatures=" + creatures + '}';
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 23 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 23 * hash + (this.name != null ? this.name.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Region other = (Region) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        return true;
    }
}
